package everyday.nowcoder;

/**
 * @Author: wenjingyuan
 * @Date: 2022/09/29/19:30
 * @Description:反转链表
 * https://www.nowcoder.com/practice/
 * 75e878df47f24fdc9dc3e400ec6058ca?tpId=295&tqId=23286&ru=/exam/
 * oj&qru=/ta/format-top101/question-ranking&sourceUrl=%2Fexam%2
 * Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%
 * 25AF%2587%26topicId%3D295
 */
public class NumBM1 {
    public ListNode ReverseList(ListNode head) {
        //新链表
        ListNode newHead =null;
        while(head!=null){
            //先保存一下访问节点的下一个节点
            //留着到下一步访问
            ListNode temp=head.next;
            //把新链表挂在原来链表的后面就可以了
            head.next=newHead;
            //更新新链表的头结点
            newHead=head;
            //继续到下一步
            head=temp;
        }
        return newHead;
    }
    }
